package dapps.web.proj;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;

import dapps.entity.proj.Proj;
import dapps.service.proj.PlanManager;
import dapps.service.proj.ProjManager;


@SuppressWarnings("serial")
@Results( { 
	@Result(name = CRUDActionSupport.RELOAD, location = "applicatesearch.action?page.pageRequest=${page.pageRequest}", type = "redirect")
	})
public class ApplicatesearchAction extends CRUDActionSupport<Proj> {

	@Autowired
	private ProjManager projManager;
	@Autowired
	private PlanManager planManager;
	// 基本属性
	private Proj entity;
	private Long id;
	
	private Long typeid;
	private List<Object> yeartimelist;
	private Integer iyeartime;
	private Page<Proj> page = new Page<Proj>(14);//每页5条记录
	
	
	// 角色相关属性

	// 基本属性访问函数 //

	public Proj getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		


		if (id != null) {
			entity = projManager.get(id);
			if(entity.getType()!=null) typeid = entity.getType().getId();
		} else {
			entity = new Proj();
		}

	}

	public void setId(Long id) {
		this.id = id;
	}
	public void setTypeid(Long typeid) {
		this.typeid = typeid;
	}
	public Page<Proj> getPage() {
		return page;
	}

	
	// CRUD Action 函数 //

	@Override

	public String list() throws Exception {

		if(yeartimelist==null||yeartimelist.isEmpty()){
			Criterion[] cs ={Restrictions.ge("yeartime", 0)
							,Restrictions.ge("status", 5)};
			Order[] os ={Order.desc("yeartime")};
			yeartimelist=planManager.getDistinctlist("yeartime", cs, os);
		}
		
		List<Criterion> criterionlist = new ArrayList<Criterion>();
		criterionlist.add(Restrictions.eq("dutycomp.id", SpringSecurityUtils.getCurrentUser().getId()));		
		criterionlist.add(Restrictions.eq("yeartime",iyeartime));
		criterionlist.add(Restrictions.eq("prorank",0));
		
		HttpServletRequest request = Struts2Utils.getRequest();
		String title = request.getParameter("filter_LIKE_title");
		if(StringUtils.isNotBlank(title)) criterionlist.add(Restrictions.like("title", title, MatchMode.ANYWHERE));

		page = projManager.SearchCriterion(page,criterionlist.toArray(new Criterion[criterionlist.size()]));
		
		return SUCCESS;
	}
	
	@Override
	public String input() throws Exception {				
		return INPUT;
	}

	@Override
	public String save() throws Exception {

		return RELOAD;
	}

	
	@Override
	public String delete() throws Exception {

		return RELOAD;
	}


	public Long getTypeid() {
		return typeid;
	}

	public List<Object> getYeartimelist() {
		return yeartimelist;
	}


	public Integer getIyeartime() {
		return iyeartime;
	}

	public void setIyeartime(Integer iyeartime) {
		this.iyeartime = iyeartime;
	}


}
